function showParticipants() {
	console.log('Showing!');
	var participants = gapi.hangout.getParticipants();
	
	var state = gapi.hangout.data.getState();
	for( var key in state ) {
		console.log(key + ' : ' + state[key]);
	}
	var retVal = '<p>Participants: </p><ul>';

	for (var index in participants) {
		var participant = participants[index];

		if (!participant.person) {
			retVal += '<li>A participant not running this app</li>';
		}
		
		var aiki = JSON.parse(state[participant.person.id]);
		retVal += '<li>' + participant.person.displayName + ' ' + aiki + '<input class="button" type="button" value="Add Aiki!" id="'+participant.person.id+'_aiki_button" onClick="addAikiTo(this.value)" value="'+participant.person.id+'"/>' + '</li>';
	}

	retVal += '</ul>';

	var div = document.getElementById('participantsDiv');

	div.innerHTML = retVal;
}

function hideParticipants() { 
	console.log('Hiding!');
	
	var div = document.getElementById('participantsDiv');
	div.innerHTML = '';
}

function addAikiTo(id) {
	var state = gapi.hangout.data.getState();
	console.log(id + " : " + state[id]);
	var aiki = state[id];
	console.log('aiki for ' + id + ' is a ' +aiki.typeof);
	aiki ++;
	aiki = JSON.stringify(aiki);
	gapi.hangout.data.setValue(id, aiki);
	
	showParticipants();
}

function init() {
  // When API is ready...                                                         
  gapi.hangout.onApiReady.add(
      function(eventObj) {
        if (eventObj.isApiReady) {
			document.getElementById('showParticipants')
            .style.visibility = 'visible';
			
			document.getElementById('hideParticipants')
            .style.visibility = 'visible';
        }
      });
	  
	gapi.hangout.onEnabledParticipantsChanged.add(
			function (eventObj) {
				var participants = eventObj.enabledParticipants;
				for( var index in participants) {
					var participant = participants[index];
					console.log(participant.person.displayName + ' ' + participant.person.id);
					var key = participant.person.id;
					var value = JSON.stringify(0);
					gapi.hangout.data.setValue(participant.person.id, value);
				}
			}
		);
}

// Wait for gadget to load.                                                       
gadgets.util.registerOnLoadHandler(init);